$(function() {
        initTable();
        initChildrenTable();
});

/**
* 初始化表格
*/
function initTable() {
$('#tracelogTable').DataTable({
language: Language,
columns: [
        {
                "data": "spanId",
                "title": "链路ID"
        },
        {
                "data": "parentSpanId",
                "title": "父链路ID"
        },
        {
                "data": "serviceName",
                "title": "服务名称"
        },
        {
        "data": "name",
        "title": "任务名称"
        },
        {
        "data": "resultFlag",
        "title": "结果",
        "render": function (data, type, row, meta) {
                if (data == "0") {
                        return "成功"
                } else if (data == "1") {
                        return "失败"
                } else {
                        return "部分成功"
                }
        }
        },
        {
                "data": "startTime",
                "title": "开始时间"
        },
        {
                "data": "endTime",
                "title": "结束时间"
        },
        {
                "data": "elapsedTime",
                "title": "耗时(ms)"
        },
        {
                "data": "null",
                "title": "操作",
                "render": function (data, type, row, meta) {
                        return `<button class="btn btn-primary" style="margin-right: 5px" data-toggle="modal" data-target="#detailModal" onClick="detailShow(this)">详情</button>` +
                            `<button class="btn" data-toggle="modal" data-target="#childrenModal"   onClick="traceLogChildren(this)">子任务</button>`
                }
        }
],
dom: 'rt<"bottom"ilp><"clear">',
destroy : true,
searching: false,
processing : true,
serverSide: false,
bSort: false,
autoWidth: false,
lengthMenu: [100],


ajax: {
type: "post",
contentType: "application/json",
url: searchUrl,
dataFilter: dataListFilterFunc,
data: function(param) {
        let json = $("#tracelogForm").serializeObject()
return JSON.stringify(json)
},
error: errorFunc
}
});
}

function initChildrenTable() {
        $('#childrenTracelogTable').DataTable({
                language: Language,
                columns: [
                        {
                                "data": "spanId",
                                "title": "链路ID"
                        },
                        {
                                "data": "serviceName",
                                "title": "服务名称"
                        },
                        {
                                "data": "name",
                                "title": "任务名称"
                        },
                        {
                                "data": "resultFlag",
                                "title": "结果",
                                "render": function (data, type, row, meta) {
                                        if (data == "0") {
                                                return "成功"
                                        } else if (data == "1") {
                                                return "失败"
                                        } else {
                                                return "部分成功"
                                        }
                                }
                        },
                        {
                                "data": "startTime",
                                "title": "开始时间"
                        },
                        {
                                "data": "endTime",
                                "title": "结束时间"
                        },
                        {
                                "data": "elapsedTime",
                                "title": "耗时(ms)"
                        }
                ],
                dom: 'rt<"bottom"ilp><"clear">',
                destroy : true,
                searching: false,
                processing : true,
                serverSide: false,
                bSort: false,
                autoWidth: false,
                lengthMenu: [100],


                ajax: {
                        type: "post",
                        contentType: "application/json",
                        url: searchChildrenUrl,
                        dataFilter: dataListFilterFunc,
                        data: function(param) {
                                let json = $("#parentForm").serializeObject()
                                return JSON.stringify(json)
                        },
                        error: errorFunc
                }
        });
}

/**
* 查询
*/
function search() {
reloadTable('tracelogTable');
}

function searchChildren() {
        reloadTable('childrenTracelogTable');
}


function exportJson() {
        let param = $("#tracelogForm").serializeObject();
        $.ajax({
                url: '/ui/tracelog/exportJson',
                async: false,
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(param),
                success: function (response) {
                        if (response.success) {
                                let data = response.data;
                                console.log(data)

                                if (data == null) {
                                        alert("没有可导出的数据")
                                } else {

                                        downloadJson("ExportJson.json", data)
                                }
                        } else {
                                alert("导出JSON失败")
                        }
                },
                error: function (xhr, textStatus, error) {
                        alert("导出JSON失败")
                }
        });
}

function downloadJson(fileName, json) {
        const jsonStr = (json instanceof Object) ? JSON.stringify(json) : json;

        const url = window.URL || window.webkitURL || window;
        const blob = new Blob([jsonStr]);
        const saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
        saveLink.href = url.createObjectURL(blob);
        saveLink.download = fileName;
        saveLink.click();
};

function detailShow(_this) {
        let tr = $(_this).parent().parent();
        let row = getRowDataTable('tracelogTable', tr);
        loadForm("detailForm", row)
}

function detailChildrenShow(_this) {
        let tr = $(_this).parent().parent();
        let row = getRowDataTable('childrenTracelogTable', tr);
        loadForm("detailForm", row)
}

function traceLogChildren(_this) {
        let tr = $(_this).parent().parent();
        let row = getRowDataTable('tracelogTable', tr);
        loadForm("parentForm", row)

        searchChildren();
}






